const PublicModel = require("@/common/public.model");
const { TenantContext } = require("@/core/tenant-context");

/**
 * 单据模型
 */
class ProductModel extends PublicModel {
    /**
     * 查询商品列表
     * @param {*} param0 
     * @returns 
     */
    queryProductList({ keyword, category_id, page, page_size }) {
        const limit = this.pageLimit(page_size, page);
        const SQL = `
            SELECT
                p.id, p.category_id, c.name category, p.name, p.code, p.barcode, p.spec, 
                p.unit_id, u.name unit, p.price, p.cost_price, p.discount_price, 
                p.image_id, p.description, p.status, p.min_stock, 
                p.created_by, p.created_at
            FROM
                products p
                LEFT JOIN product_category c
                ON c.id = p.category_id
                LEFT JOIN product_unit u
                ON u.id = p.unit_id 
            WHERE
                p.is_delete = 0 
                ${keyword ? ` AND p.name LIKE '%${keyword}%' ` : ''} 
                ${category_id ? ` AND p.category_id='${category_id}' ` : ''} 
                AND p.tenant_id = '${TenantContext.getCurrentTenant()}' 
                AND u.is_delete = 0  
                AND c.is_delete = 0 
            LIMIT ${limit}
        `;
        return this.query(SQL)
    }

    /**
     * 获取单个商品
     * @param {*} id 
     * @returns 
     */
    getProduct(id) {
        return this.findById({ table: 'products', id: id });
    }

    /**
     * 获取商品总数
     * @param {*} param0 
     * @returns 
     */
    getProductTotal({ keyword, category_id, page, pageSize }) {
        const SQL = this.injectTenantCondition(`
            SELECT count(id) as count FROM products WHERE
                is_delete = 0 
                ${keyword ? ` AND name LIKE '%${keyword}%' ` : ''} 
                ${category_id ? ` AND category_id='${category_id}' ` : ''} 
        `);
        return this.query(SQL);
    }


    // 通过名称获取产品
    getProductByName({ name, tid }) {
        const SQL = this.whereAnd(`SELECT * FROM products`, {
            is_delete: 0,
            name: name,
            tenant_id: TenantContext.getCurrentTenant()
        });
        return this.query(SQL);
    }
    // 创建产品
    createProduct(data) {
        return this.create({
            table: 'products',
            data: data,
            result: true
        });
    }
    // 删除产品
    deleteProduct({ id, tid }) {
        return this.deleteById({ table: 'products', id: id, tid: tid });
    }
    // 修改产品
    updateProduct({ id, data }) {
        return this.updateById({ table: 'products', id: id, data: data });
    }
    // 创建产品类别
    createProductCategory(data) {
        return this.create({
            table: 'product_category',
            data: data,
            result: true
        });
    }
    // 查询产品类别列表
    queryProductCategoryList() {
        const SQL = this.injectTenantCondition(`
            SELECT
                id, name, code, sort, icon, description, created_at, created_by
            FROM
                product_category
            WHERE
                is_delete = 0
        `);
        return this.query(SQL);
    }

    // 删除类别
    deleteProductCategory(id) {
        return this.deleteById({ table: 'product_category', id: id });
    }

    // 修改商品单位
    updateProductCategory(id, data) {
        return this.updateById({
            table: 'product_category',
            id: id,
            data: data || {}
        });
    }

    // 创建商品单位
    createProductUnit(data) {
        return this.create({
            table: 'product_unit',
            data: data,
            result: true
        });
    }
    
    // 查询单位列表
    queryProductUnitList() {
        const SQL = this.injectTenantCondition(`SELECT id, name, remark FROM product_unit WHERE is_delete = 0 LIMIT 100`);
        return this.query(SQL);
    }

    // 修改商品单位
    updateProductUnit(id, data) {
        return this.updateById({
            table: 'product_unit',
            id: id,
            data: data || {}
        });
    }

    // 删除单位
    deleteProductUnit(id) {
        return this.deleteById({ table: 'product_unit', id: id });
    }

    // 查询单个单位
    getProductUnit(id) {
        return this.findById({ table: 'product_unit', id: id })
    }

    // 查询单个单位
    getProductCategory(id) {
        return this.findById({ table: 'product_category', id: id })
    }
}

module.exports = new ProductModel();
